<?php
/**
 * SweetPlurk
 *
 * @package    SweetPlurk
 * @copyright  Copyright (c) 2008-2012 Wabow Information Inc. (http://www.wabow.com)
 * @license    New BSD License
 * @version    $Id$
 */

/**
 * User 類別
 *
 * @package    SweetPlurk
 * @copyright  Copyright (c) 2008-2012 Wabow Information Inc. (http://www.wabow.com)
 * @license    New BSD License
 */
class SweetPlurk_Users extends SweetPlurk_Go implements SeekableIterator, Countable, ArrayAccess
{
    /**
     * 指標
     *
     * @var int
     */
    protected $_pointer = 0;

    /**
     * 總數
     *
     * @var int
     */
    protected $_count = 0;

    /**
     * 資料列表
     *
     * @var array
     */
    protected $_list = array();

    /**
     * 建構
     *
     * @param object $userData
     */
    public function __construct($userData)
    {
        parent::__construct($userData);
        $this->_init();
    }

    /**
     * 初始化
     *
     */
    protected function _init()
    {
        foreach ($this->plurk_users as $userData) {
            $this->_list[] = new SweetPlurk_User($userData);
        }
        $this->_count = count($this->_list);
    }

    /**
     * 重置指標
     *
     * @return SweetPlurk_Users
     */
    public function rewind()
    {
        $this->_pointer = 0;
        return $this;
    }

    /**
     * 回傳目前指標指向的 User
     *
     * @return SweetPlurk_User
     */
    public function current()
    {
        if ($this->valid() === false) {
            return null;
        }

        if (empty($this->_list[$this->_pointer])) {
            $this->_list[$this->_pointer] = new SweetPlurk_User(array(
                'nick_name' => null,
                'has_profile_image' => null,
                'location' => null,
                'date_of_birth' => null,
                'avatar' => null,
                'full_name' => null,
                'gender' => null,
                'timezone' => null,
                'id' => null,
                'karma' => null,
            ));
        }

        return $this->_list[$this->_pointer];
    }

    /**
     * 回傳指標
     *
     * @return int
     */
    public function key()
    {
        return $this->_pointer;
    }

    /**
     * 指標指向下一筆
     *
     * @return void
     */
    public function next()
    {
        ++$this->_pointer;
    }

    /**
     * 檢查指標是否超過總數
     *
     * @return bool
     */
    public function valid()
    {
        return $this->_pointer < $this->_count;
    }

    /**
     * 移動指標
     *
     * @param int $position
     * @return SweetPlurk_Users
     */
    public function seek($position)
    {
        $position = (int) $position;
        if ($position < 0 || $position >= $this->_count) {
            throw new Exception("Illegal index $position");
        }
        $this->_pointer = $position;
        return $this;
    }

    /**
     * User 總數
     *
     * @return int
     */
    public function count()
    {
        return $this->_count;
    }

    /**
     * 特定 User 是否存在
     *
     * @param int $offset
     * @return bool
     */
    public function offsetExists($offset)
    {
        return isset($this->_list[(int) $offset]);
    }

    /**
     * 取得目前指標指向的 User
     *
     * @param int $offset
     * @return SweetPlurk_User
     */
    public function offsetGet($offset)
    {
        $this->_pointer = (int) $offset;
        return $this->current();
    }

    /**
     * 不做任何事
     *
     * @param int $offset
     * @param mixed $value
     */
    public function offsetSet($offset, $value)
    {
    }

    /**
     * 不做任何事
     *
     * @param int $offset
     */
    public function offsetUnset($offset)
    {
    }
}